package cn.sdstudio.ai.controller;

import cn.sdstudio.ai.entity.vo.MessageVo;
import cn.sdstudio.ai.respository.ChatHistoryRepository;
import cn.sdstudio.model.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;
    private final ChatMemory chatMemory;

    @GetMapping("/{type}")
    public Result<List<String>> getChatIds(@PathVariable String type){
            return Result.success(chatHistoryRepository.getChatIds(type));
    }

    @GetMapping("/{type}/{chatId}")
    public Result<List<MessageVo>> getChatIds(@PathVariable("type") String type,@PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
        if(messages == null){
            return Result.success(List.of());
        }
      return Result.success( messages.stream().map(MessageVo::new).toList());

    }
}
